package com.woquyin.controller.api;

import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.json.FastJson;
import com.woquyin.common.Constance;
import com.woquyin.interceptor.ApiAuthInterceptor;
import com.woquyin.interceptor.ParamInterceptor;
import com.woquyin.model.SecUser;
import com.woquyin.vo.ReturnData;
import com.woquyin.vo.user.LoginVo;
import com.woquyin.vo.user.SignVo;
import com.woquyin.vo.user.UpdateVo;

@Before(ApiAuthInterceptor.class)
public class UserController extends Controller {
	
	@Clear(ApiAuthInterceptor.class)
	public void index(){
		renderJsp("index.jsp");
	}

	@Clear(ApiAuthInterceptor.class)
	public void toSign() {
		renderJsp("register.jsp");
	}

	@Clear(ApiAuthInterceptor.class)
	public void sign() {
		String jsonString = getAttrForStr(ParamInterceptor.REQUEST_JSON);
		SignVo signVo = FastJson.getJson().parse(jsonString, SignVo.class);
		renderJson(SecUser.dao.signService(signVo));
	}

	@Clear(ApiAuthInterceptor.class)
	public void toLogin(){
		renderJsp("login.jsp");
	}

	@Clear(ApiAuthInterceptor.class)
	public void login() {
		ReturnData<SecUser> returnData = new ReturnData<SecUser>();
		String jsonString = getAttrForStr(ParamInterceptor.REQUEST_JSON);
		LoginVo vo = FastJson.getJson().parse(jsonString, LoginVo.class);
		returnData = SecUser.dao.loginService(vo);
		if(returnData.getCode() == Constance.WholeStateCodeEnum.SUCCESS.getCode()){
			getSession().setAttribute("userInfo", returnData.getData());
		}
		renderJson(returnData);
	}
	
	public void get() {
		setAttr("data", getSession().getAttribute("userInfo"));
		renderJsp("userInfo.jsp");
	}

	public void update() {
		ReturnData<SecUser> resp = new ReturnData<SecUser>();
		String jsonString = getAttrForStr(ParamInterceptor.REQUEST_JSON);
		UpdateVo vo = FastJson.getJson().parse(jsonString, UpdateVo.class);
		SecUser userInfo = (SecUser) getSession().getAttribute("userInfo");
		renderJson(SecUser.dao.updateUserInfoService(vo, userInfo));
	}

//	public void myCenter() {
//		ReturnData<SecUser> resp = new ReturnData<SecUser>();
//		String jsonString = getAttrForStr(ParamInterceptor.REQUEST_JSON);
//		HashMap<String, String> data = FastJson.getJson().parse(jsonString, HashMap.class);
//		String token = data.get("token");
//		SecUser user = SecUser.dao.findByToken(token);
//		if (user != null) {
//			resp.setData(user);
//		}
//		renderJson(resp);
//	}
	
//	public void logout() {
//	ReturnData<String> resp = new ReturnData<String>();
//	String jsonString = getAttrForStr(ParamInterceptor.REQUEST_JSON);
//	HashMap<String, String> data = FastJson.getJson().parse(jsonString, HashMap.class);
//	String token = data.get("token");
//	CacheKit.remove(SecUser.CACHE_NAME_LOGINED_USER, token);
//	resp.setMsg("退出成功");
//	renderJson(resp);
//}
}
